package com.zrrd.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.zrrd.gmall.item.service.ItemService;
import com.zrrd.gmall.list.fegin.ListFefinClient;
import com.zrrd.gmall.model.product.BaseCategoryView;
import com.zrrd.gmall.model.product.SkuInfo;
import com.zrrd.gmall.model.product.SpuSaleAttr;
import com.zrrd.gmall.product.fegin.ProductFeginClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeginClient productFeginClient;
    @Autowired
    private ListFefinClient listFefinClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    //汇总数据的方法
    @Override
    public Map<String, Object> getSkuById(Long skuId) {

        //创建一个最终返回的Map集合
        Map<String, Object> map = new HashMap<>();

        //1.获取Sku基本信息
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeginClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        //2.获取Sku的分类信息
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView baseCategoryView = productFeginClient.getBaseCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);

        //3.获取Sku的最新价格
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                BigDecimal price = productFeginClient.getSkuPrice(skuId);
                map.put("price", price);
            }
        }, threadPoolExecutor);

        //4.根据SkuId和SpuId查询出sku对应的spu的销售属性以及属性值以及标记当前sku所选中的销售属性
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> saleAttrListBySkuId = productFeginClient.getSkuSaleAttrListBySkuId(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", saleAttrListBySkuId);
        }, threadPoolExecutor);

        //5.根据SpuId获取前端需要的切换商品的数据
        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map idsMap = productFeginClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String mapJson = JSON.toJSONString(idsMap);
            map.put("valuesSkuJson", mapJson);
        }, threadPoolExecutor);

        //6.更新商品的热度
        CompletableFuture<Void> incrHotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFefinClient.incrHotScore(skuId);
        }, threadPoolExecutor);

        //组合以上6个任务 并且执行
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                categoryViewCompletableFuture,
                priceCompletableFuture,
                spuSaleAttrListCompletableFuture,
                valuesSkuJsonCompletableFuture,
                incrHotScoreCompletableFuture
        ).join();


        System.out.println("汇总的Map===>" + map);
        return map;
    }
}
